package pl.edu.pk.mech.kkbus.controller;

import java.util.List;
import java.util.logging.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import pl.edu.pk.mech.kkbus.model.Award;
import pl.edu.pk.mech.kkbus.model.ResponseStatus;
import pl.edu.pk.mech.kkbus.service.AwardService;

@Controller
public class AwardController extends RestController {

	@Autowired
	AwardService awardServices;
	
    private static final Logger log = Logger.getLogger(AwardController.class.getName());
    
    @RequestMapping(value = "/awards", method = RequestMethod.GET)
    public @ResponseBody List<Award> showAwards(@RequestParam(required = false, defaultValue = "-1") int page) {
        if (page < 0) {
            log.info("Get all awards");
            return awardServices.getAllAwards();
        } else {        
            log.info("Get part awards. Page: " + page);
            return null;
        }         
    }
    
    @RequestMapping(value = "/award", method = RequestMethod.POST)
    public @ResponseBody ResponseStatus createAward(@RequestBody Award award) {
    	boolean result = awardServices.createOrUpdateAward(award);
        return result ? ResponseStatus.ADDED : ResponseStatus.ERROR;
    }
    
    @RequestMapping(value = "/award/{id}", method = RequestMethod.PUT)
    public @ResponseBody ResponseStatus updateAward(@PathVariable String id, @RequestBody Award award) {
    	boolean result = awardServices.createOrUpdateAward(award);
        return result ? ResponseStatus.UPDATED : ResponseStatus.ERROR;
    }
    
    @RequestMapping(value = "/award/{id}", method = RequestMethod.DELETE)
    public @ResponseBody ResponseStatus deleteAward(@PathVariable String id) {
        // TODO Call service method
    	boolean result = false;
        return result ? ResponseStatus.DELETED : ResponseStatus.ERROR;
    }
}
